package com.tensquare.user.controller;

import com.tensquare.user.pojo.User;
import com.tensquare.user.service.UserService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.ipc.netty.http.server.HttpServerRequest;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private HttpServletRequest request;
    //1.发送短信验证码
    @PostMapping("sendsms/{mobile}")
    public Result sendSms(@PathVariable String mobile){
        // 1.1 发送
        userService.sendSms(mobile);
        return new Result(true,StatusCode.OK,"验证码发送成功");
    }
    //2. 用户注册
    @PostMapping("register/{code}")
    public Result redoster(@PathVariable String code, @RequestBody User user){
        System.out.println("正在执行usercotroller");
        boolean register = userService.register(code, user);
        if (register){
            jwtUtil.createJWT(user.getId(),user.getNickname(),"user");
            return new Result(true,StatusCode.OK,"注册用户成功");
        }
        return new Result(true,StatusCode.OK,"注册用户失败");
    }
    //3. 用户登录
    @PostMapping("login")
    public Result login(@RequestBody User user){
        //3.1 根据当前用户手机查询是否存在用户，存在就比较密码是否相等
        User loginUser = userService.login(user);
        if (loginUser != null){
            String token = jwtUtil.createJWT(user.getId(),user.getNickname(),"user");
            if (StringUtils.isNotBlank(token)){
                Map<String,String> map = new HashMap<>();
                map.put("token",token);
                map.put("nickname",user.getNickname());
                map.put("id",user.getId());
                return new Result(true,StatusCode.OK,"用户登录成功",map);
            }
        }
        return new Result(false,StatusCode.ERROR,"用户登录失败");
    }
    //4. 删除用户
    @DeleteMapping("{userId}")
    public Result dele(@PathVariable String userId){
        //4.1 先从request中得到claims
        Claims claims = (Claims) request.getAttribute("claims");
        //4.2 判断是否存在，存在就取出roles
        if (claims != null){
            String roles = (String) claims.get("roles");
            if (StringUtils.isNotBlank(roles) && roles.equals("admin"));
            userService.dele(userId);
            return new Result(true,StatusCode.OK,"删除用户成功");
        }
        return new Result(false,StatusCode.ERROR,"没有权限");

    }
    //5.修改粉丝数
    @PutMapping("/incfans/{userid}/{x}")
    public Result incfans(@PathVariable String userid,@PathVariable int x){
        userService.incFans(userid,x);
        return new Result(true,StatusCode.OK,"修改粉丝成功");
    }
    //6. 修改关注数
    @PutMapping("/incfollow/{userid}/{x}")
    public Result incfollow(@PathVariable String userid,@PathVariable int x){
        userService.incFollow(userid,x);
        return new Result(true,StatusCode.OK,"修改关注成功");

    }

}
